package com.ytjj.common.utils.locker;

import org.redisson.api.RLock;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 redis分布式锁工具类

 @since 2021年9月3日16:39:44
 */
@Service
public class RedisLockUtil {

    @Resource
    private DistributedLocker distributedLocker;

    /**
     释放锁
     @param lockKey 锁key
     */
    public void unlock(String lockKey) {
        distributedLocker.unlock(lockKey);
    }

    /**
     尝试获取锁
     @param lockKey 锁key
     @param waitTime 最多等待时间
     @param leaseTime 上锁后自动释放锁时间
     @return boolean
     */
    public boolean tryLock(String lockKey, int waitTime, int leaseTime) {
        return distributedLocker.tryLock(lockKey, TimeUnit.SECONDS, waitTime, leaseTime);
    }
}
